function [out]=Fleet_v1(x_aveprofit)
% x = equal ave profit across all patches

global Bi_legal_fish pr delta theta Fsum

% INVERSE MODEL
% warning off MATLAB:divideByZero
% Fi_vector_fleet = (Bi_legal_fish.*pr-x_aveprofit-x_aveprofit*delta-theta-delta*theta)./(x_aveprofit+theta);
% warning on MATLAB:divideByZero
% Fi_vector_fleet=max(Fi_vector_fleet,0);
% out=(sum(Fi_vector_fleet,1)-Fsum);

%EXPONENTIAL MODEL
warning off MATLAB:divideByZero
% Q=(x_aveprofit+theta)./(pr+Bi_legal_fish); %pr PLUS Bi_legal_fish.......WRONG, SEE FleetModel_exp_v2.nb Mathematica code
Q=(x_aveprofit+theta)./(pr*Bi_legal_fish); %pr MULTIPLIED BY Bi_legal_fish.......RIGHT
lambert_z=-exp(-1./Q)./Q;
warning on MATLAB:divideByZero
% whos lambert_z
%wapr will break if lambert_z is out of range (i.e., <-exp(-1)); so set
%those values to the edge of the range = -exp(-1)
lambert_z(lambert_z<-exp(-1))=-exp(-1);
[lambert_w nerror]=wapr(lambert_z);
% whos Q lambert_w
d_plus_Fi=1./Q+lambert_w';
d_plus_Fi=max([zeros(length(d_plus_Fi),1) d_plus_Fi],[],2); %get ride of tiny negative results
Fi_vector_fleet=d_plus_Fi-delta;
Fi_vector_fleet=max(Fi_vector_fleet,0);
out=(sum(Fi_vector_fleet,1)-Fsum);











% Find aveProfit manually - INVERSE MODEL
% clear out Fvector_sum loc
% xrange=[0:1:10];
% % xrange=[0:1e-8:1e-6];
% xi=0;
% for x=xrange
%     xi=xi+1;
% %     Fi_vector=(Bi_legal_fish./(x+theta))-1-delta;
%     Fi_vector = (Bi_legal_fish.*pr-x-x*delta-theta-delta*theta)./(x+theta);
%     Fi_vector=max(Fi_vector,0);
%     out(xi)=(sum(Fi_vector,1)-Fsum);
%     Fvector_sum(xi)=sum(Fi_vector);
% end
% loc=find(out.^2==min(out.^2));
% aveProfitfzero=xrange(loc(1));
% Fi_vector_soln = (Bi_legal_fish.*pr-xrange(loc(1))-xrange(loc(1))*delta-theta-delta*theta)./(xrange(loc(1))+theta);
% disp(['Fleet model ave profit = ',num2str(xrange(loc)),])
% figure
% plot(xrange,out); xlabel('Average profit'); 
% hold on
% plot(xrange,Fvector_sum,'r')
% plot(xrange,Fsum,'g')
% legend('Difference','sum(Fi)','Fbar')
% title(['Fleet model ave profit = ',num2str(xrange(loc(1))),])
% grid on